package com.ln.frame.es;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.*;

public class EsDemo {

	public static void main(String[] args) throws Exception {
		restHighLevelClientDemo();
//		restLowLevelClientDemo();
//		preBuiltXPackTransportClientDemo();
	}

	public static void preBuiltXPackTransportClientDemo() throws UnknownHostException {
		Settings settings = Settings.builder()
				.put("xpack.security.user", "elastic:zfnAsZTtYNmNqgWO1eoR")
				.put("client.transport.sniff", true) // 自动嗅探
				.put("client.transport.ping_timeout", "5s")    // ping 节点的响应时间
				.put("client.transport.nodes_sampler_interval", "5s")    // 节点的时间间隔
				.build();

		TransportClient client = new PreBuiltXPackTransportClient(settings, Collections.emptyList(), (node, ex) -> {
			System.out.printf("节点: %s, 异常: %s \n", node, ex);
		});
		client.addTransportAddress(new TransportAddress(InetAddress.getByName("172.16.21.185"), 9200));
		client.addTransportAddress(new TransportAddress(InetAddress.getByName("172.16.21.186"), 9200));
		client.addTransportAddress(new TransportAddress(InetAddress.getByName("172.16.21.187"), 9200));


		System.out.println(client.connectedNodes());

		client.close();
	}

	public static void restHighLevelClientDemo() throws IOException {
		final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "zfnAsZTtYNmNqgWO1eoR"));  //es账号密码
		RestHighLevelClient client = new RestHighLevelClient(
				RestClient.builder(
								new HttpHost("172.16.21.185", 9200, "http"),
								new HttpHost("172.16.21.186", 9200, "http"),
								new HttpHost("172.16.21.187", 9200, "http")
						)
						.setHttpClientConfigCallback(httpClientBuilder -> {
							httpClientBuilder.disableAuthCaching();
							return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
						}));
		System.out.println(client);
		
		// 插入数据
		String index = "tp-dev-log4j";
		BulkRequest request = new BulkRequest();
		Map<String, Object> map = new HashMap<>();
		map.put("info", "测试 - " + UUID.randomUUID().toString());
		map.put("level", "info");
		request.add(new IndexRequest(index).type(null).create(false).source(map));
		BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
		System.out.println("失败? " + bulk.hasFailures());
		
		client.close();
	}

	public static void restLowLevelClientDemo() throws IOException {
		final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "zfnAsZTtYNmNqgWO1eoR"));  //es账号密码

		RestClient restClient = RestClient.builder(
				new HttpHost("172.16.21.185", 9200, "http"),
				new HttpHost("172.16.21.186", 9200, "http"),
				new HttpHost("172.16.21.187", 9200, "http"))
				.setHttpClientConfigCallback(httpClientBuilder -> {
					httpClientBuilder.disableAuthCaching();
					return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
				})
				.build();

		System.out.println(restClient);

		restClient.close();
	}

}
